/**
 ******************************************************************************
 * @file    crgreg.h
 * @author  hyseim software Team
 * @date    18-Aug-2023
 * @brief   This file provides all the headers of the crgreg functions.
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2020 Hyseim. Co., Ltd.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __CSR_REG_H__
#define __CSR_REG_H__

#define CSRS(add, n) asm volatile ("csrs "#add", %[op0]" :: [op0] "r" (n) : )
#define CSRC(add, n) asm volatile ("csrc "#add", %[op0]" :: [op0] "r" (n) : )
#define CSR_GET(add, n) asm volatile ("csrr %[op0], "#add : [op0] "=r" (n) : : )
#define CSR_SET(add, n) asm volatile ("csrw  "#add", %[op0]" : : [op0] "r" (n) : )
#define CSR_SETEX(a, n) CSR_SET((a), (n))
#define CSR_GETEX(a, n) CSR_GET((a), (n))
#define CSRSEX(a, n) CSRS((a), (n))
#define CSRCEX(a, n) CSRC((a), (n))

#define MSTATUS_UIE         0x00000001
#define MSTATUS_SIE         0x00000002
#define MSTATUS_HIE         0x00000004
#define MSTATUS_MIE         0x00000008
#define MSTATUS_UPIE        0x00000010
#define MSTATUS_SPIE        0x00000020
#define MSTATUS_UBE         0x00000040
#define MSTATUS_MPIE        0x00000080
#define MSTATUS_SPP         0x00000100
#define MSTATUS_VS          0x00000600
#define MSTATUS_MPP         0x00001800
#define MSTATUS_FS          0x00006000
#define MSTATUS_XS          0x00018000
#define MSTATUS_MPRV        0x00020000
#define MSTATUS_SUM         0x00040000
#define MSTATUS_MXR         0x00080000
#define MSTATUS_TVM         0x00100000
#define MSTATUS_TW          0x00200000
#define MSTATUS_TSR         0x00400000
#define MSTATUS32_SD        0x80000000

#define IRQ_U_SOFT   0
#define IRQ_S_SOFT   1
#define IRQ_VS_SOFT  2
#define IRQ_M_SOFT   3
#define IRQ_U_TIMER  4
#define IRQ_S_TIMER  5
#define IRQ_VS_TIMER 6
#define IRQ_M_TIMER  7
#define IRQ_U_EXT    8
#define IRQ_S_EXT    9
#define IRQ_VS_EXT   10
#define IRQ_M_EXT    11
#define IRQ_S_GEXT   12
#define IRQ_COP      12
#define IRQ_LCOF     13

#define MIP_USIP            (1 << IRQ_U_SOFT)
#define MIP_SSIP            (1 << IRQ_S_SOFT)
#define MIP_VSSIP           (1 << IRQ_VS_SOFT)
#define MIP_MSIP            (1 << IRQ_M_SOFT)
#define MIP_UTIP            (1 << IRQ_U_TIMER)
#define MIP_STIP            (1 << IRQ_S_TIMER)
#define MIP_VSTIP           (1 << IRQ_VS_TIMER)
#define MIP_MTIP            (1 << IRQ_M_TIMER)
#define MIP_UEIP            (1 << IRQ_U_EXT)
#define MIP_SEIP            (1 << IRQ_S_EXT)
#define MIP_VSEIP           (1 << IRQ_VS_EXT)
#define MIP_MEIP            (1 << IRQ_M_EXT)
#define MIP_SGEIP           (1 << IRQ_S_GEXT)
#define MIP_LCOFIP          (1 << IRQ_LCOF)

#define CSR_FFLAGS  		0x001
#define CSR_FRM  			0x002
#define CSR_FCSR  			0x003
#define CSR_MSTATUS  		0x300
#define CSR_MISA  			0x301
#define CSR_MIE  			0x304
#define CSR_MTVEC  			0x305
// #define CSR_PERFVG  			0x317
#define CSR_MCOUNTINHIBIT  	0x320
#define CSR_MHPME3  		0x323
#define CSR_MHPME4  		0x324
#define CSR_MHPME5  		0x325
#define CSR_MHPME6  		0x326
#define CSR_MSCRATCH  		0x340
#define CSR_MEPC  			0x341
#define CSR_MCAUSE  		0x342
#define CSR_MTVAL  			0x343
#define CSR_MIP  			0x344
#define CSR_MTSEL  		    0x7a0
#define CSR_MTDATA1  		0x7a1
#define CSR_MTDATA2  		0x7a2
#define CSR_DCSR  			0x7b0
#define CSR_DPC  			0x7b1
#define CSR_MRAC  			0x7c0
#define CSR_MCPC  			0x7c2
#define CSR_DMST  			0x7c4
#define CSR_MPMC  		    0x7c6
#define CSR_DICAWICS  		0x7c8
#define CSR_DICAD0  		0x7c9
#define CSR_DICAD1  		0x7ca
#define CSR_DICAGO  		0x7cb
#define CSR_DICAD0H  		0x7cc
#define CSR_MFDHT  			0x7ce
#define CSR_MFDHS  			0x7cf
#define CSR_MITCNT0  		0x7d2
#define CSR_MITB0  			0x7d3
#define CSR_MITCTL0  		0x7d4
#define CSR_MITCNT1  		0x7d5
#define CSR_MITB1  			0x7d6
#define CSR_MITCTL1  		0x7d7
#define CSR_MICECT  		0x7f0
#define CSR_MICCMECT  		0x7f1
#define CSR_MDCCMECT  		0x7f2
#define CSR_MCGC  			0x7f8
#define CSR_MFDC  			0x7f9
#define CSR_MHARTSTART  	0x7fc
#define CSR_MNMIPDEL  		0x7fe
#define CSR_MSCAUSE  		0x7ff
// not used
// #define CSR_MEICPCT  			0xbca
#define CSR_MCYCLEL  		0xb00
#define CSR_MINSTRETL  		0xb02
#define CSR_MHPMC3  		0xb03
#define CSR_MHPMC4  		0xb04
#define CSR_MHPMC5  		0xb05
#define CSR_MHPMC6  		0xb06
#define CSR_MCYCLEH  		0xb80
#define CSR_MINSTRETH  		0xb82
#define CSR_MHPMC3H  		0xb83
#define CSR_MHPMC4H  		0xb84
#define CSR_MHPMC5H  		0xb85
#define CSR_MHPMC6H  		0xb86
#define CSR_MDEAU  			0xbc0
#define CSR_MEIVT  			0xbc8
#define CSR_MEIPT  			0xbc9
#define CSR_MEICURPL  		0xbcc
#define CSR_MEICIDPL  		0xbcb
// #define CSR_PERFVA  			0xb07
// #define CSR_PERFVD  			0xb87
#define CSR_MVENDORID  		0xf11
#define CSR_MARCHID  		0xf12
#define CSR_MIMPID  		0xf13
#define CSR_MHARTID  		0xf14
#define CSR_MDSEAC  		0xfc0
#define CSR_MHARTNUM  		0xfc4
#define CSR_MEIHAP  		0xfc8

#define MSTATUS_MIE_BIT     3

#endif
